package com.wanmait.baohan.controller.CustomerController;

import com.wanmait.baohan.pojo.BusinessContact;
import com.wanmait.baohan.pojo.Supplier;
import com.wanmait.baohan.service.BusinessContactService;
import com.wanmait.baohan.service.DictionaryService;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("customerInformation")
public class BusinessContactsController
{
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private BusinessContactService businessContactService;
    @RequestMapping("businessContactsDetailed")
    public ModelAndView businessContactDetailed(String id,String customerId)
    {
        ModelAndView modelAndView = new ModelAndView();
        BusinessContact businessContact = businessContactService.findOneBy(Integer.parseInt(id));
        modelAndView.addObject("businessContact",businessContact);
        modelAndView.addObject("customerId",customerId);

        return modelAndView;
    }
    @RequestMapping("businessContactsData")
    @ResponseBody
    public DataVo<BusinessContact> businessContactsData(String name,String customerName)
    {
        BusinessContact businessContact = new BusinessContact();
        businessContact.setName(name);
        if(StringUtils.hasText(customerName))
        {
            Supplier supplier = new Supplier();
            supplier.setName(customerName);
            businessContact.setSupplier(supplier);
        }
        DataVo<BusinessContact> dataVo = new DataVo<>();
        List<BusinessContact> businessContacts = businessContactService.findAllBy(businessContact);
        dataVo.setCount((long)businessContacts.size());
        dataVo.setData(businessContacts);
        return dataVo;
    }
    @RequestMapping("businessContactsList")
    public void businessContactsList()
    {

    }
    @GetMapping("addBusinessContactsByCustomer")
    public ModelAndView addBusinessContactsByCustomer()
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> dictionaryListA = dictionaryService.selectBusinessContactsTypeA();
        List<Map<String, Object>> dictionaryListB = dictionaryService.selectBusinessContactsTypeB();
        modelAndView.addObject("dictionaryListA",dictionaryListA);
        modelAndView.addObject("dictionaryListB",dictionaryListB);
        //modelAndView.addObject("supplierId",supplierId);
        dictionaryService.selectBusinessContactsTypeB();

        return modelAndView;
    }
    @PostMapping("addBusinessContactsByCustomer")
    public String addBusinessContactsByCustomer(BusinessContact businessContact)
    {
        /*System.out.println(businessContact.getSupplierId());
        System.out.println(businessContact.getDictionaryA().getId());
        System.out.println(businessContact.getAddress());
        System.out.println(businessContact.getEmail());*/
        businessContactService.insert(businessContact);
        return "redirect:/customerInformation/businessContactsList";
    }
    //业务联系人页面liu
    @GetMapping("businessContacts")
    public ModelAndView addBusinessContacts(String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> dictionaryListA = dictionaryService.selectBusinessContactsTypeA();
        List<Map<String, Object>> dictionaryListB = dictionaryService.selectBusinessContactsTypeB();
        modelAndView.addObject("dictionaryListA",dictionaryListA);
        modelAndView.addObject("dictionaryListB",dictionaryListB);
        modelAndView.addObject("supplierId",supplierId);

        dictionaryService.selectBusinessContactsTypeB();

        return modelAndView;
    }
    @PostMapping("businessContacts")
    public void addBusinessContacts(BusinessContact businessContact)
    {
        /*System.out.println(businessContact.getSupplierId());
        System.out.println(businessContact.getDictionaryA().getId());
        System.out.println(businessContact.getAddress());
        System.out.println(businessContact.getEmail());*/
        businessContactService.insert(businessContact);
    }

}
